var say = say||{};
say.account = say.account||{};
say.account.index = say.account.index||{};

//查询用户信息并展示
say.account.index.queryAccount = function(){
	var url = "/security/account/rest/query";
	$.ajax({
       	type: 'POST',
       	url: url,	//webservice
       	data: {},
       	dataType:'json',	
       	success: function (result) {	
	   		if(result && result.status){
				if(result.status==1 && result.data){
					say.account.index.account = result.data[0];
					$("#USER_INFO_NODE").html("");
					$("#USER_INFO_NODE").append(_.template($("#USER_INFO_ITEM").html())({info: say.account.index.account}));
				}
			}
       	},
       	error: function(result){
       		alert("未知错误");
       	}
   	});
}

say.account.index.saveAccount = function(){
	var url = "/security/account/rest/save";
	var formData = base.serializeObject($("#editForm"));
	formData = $.extend(say.account.index.account,formData);
	$.ajax({
	   	type: 'post',
	   	url: url,	
	   	data: JSON.stringify(formData),
	   	dataType:'json',	
	   	contentType: 'application/json',
	   	success: function (result) {	
	   		if(result){
				if(result.status==1){
					layer.msg( "修改成功",{icon:1});
					say.account.index.changeButton("#edit","#editSave");
					say.account.index.queryAccount();
				}
			}
	   	},
	   	error: function(result){
	   		layer.msg(result.msg || "未知错误",{icon:1});
	   	}
	});
}


//编辑按钮事件
say.account.index.edit = function(){
	$("#USER_INFO_NODE").html("");
	$("#USER_INFO_NODE").append(_.template($("#USER_EDIT_ITEM").html())({info: say.account.index.account}));
	say.account.index.changeButton("#editSave","#edit");
}

//参数是两个ID，第一个是需要展示，第二个是需要隐藏的
say.account.index.changeButton = function(show,hidden){
	$(show).removeClass("hidden");$(show).addClass("show");
	$(hidden).removeClass("show");$(hidden).addClass("hidden");
}


$(function(){
	say.account.index.queryAccount();
	
	//绑定事件
	$("#edit").on("click",say.account.index.edit);
	$("#editSave").on("click",say.account.index.saveAccount);
	
	base.setTitle("用户个人中心");
});